package main

import "fmt"

func main() {
	//r := lengthOfLongestSubstring("abcbcbb")
	r := lengthOfLongestSubstring("pwwkew")
	fmt.Println(r)
}

func lengthOfLongestSubstring(s string) int {
	maxLength := 0
	m := make(map[byte]int)
	i, j := 0, 0
	for ; j < len(s); j++ {
		if ri, ok := m[s[j]]; ok && ri >= i {
			maxLength = max(maxLength, j-i)
			//i++
			i = ri + 1
		}
		m[s[j]] = j
	}

	return max(maxLength, len(s)-i)
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}
